package jayson.chatui.adapter.holder;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import jayson.chatui.R;
import jayson.chatui.adapter.ChatAdapter;
import jayson.chatui.model.MessageInfo;
import jayson.chatui.util.Constants;
import jayson.chatui.util.Utils;
import jayson.chatui.widget.BubbleLinearLayout;
import jayson.chatui.widget.GifTextView;

public class ChatSendViewHolder extends BaseViewHolder<MessageInfo> {
    private static final String TAG = "ChatSendViewHolder";
    ImageView chatItemAvatar;
    GifTextView chatItemContentText;
    BubbleLinearLayout chatItemLayoutContent;
    ProgressBar chatItemProgress;
    ImageView chatItemFail;

    private ChatAdapter.onItemClickListener onItemClickListener;
    private Handler handler;
    private RelativeLayout.LayoutParams layoutParams;
    private Context mContext;

    public ChatSendViewHolder(ViewGroup parent, ChatAdapter.onItemClickListener onItemClickListener, Handler handler) {
        super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send, parent, false));
        findViewByIds(itemView);
        setItemLongClick();
        this.mContext = parent.getContext();
        this.onItemClickListener = onItemClickListener;
        this.handler = handler;
        layoutParams = (RelativeLayout.LayoutParams) chatItemLayoutContent.getLayoutParams();
    }

    private void findViewByIds(View itemView) {
        chatItemAvatar = itemView.findViewById(R.id.chat_item_header);
        chatItemContentText = itemView.findViewById(R.id.chat_item_content_text);
        chatItemLayoutContent = itemView.findViewById(R.id.chat_item_layout_content);
        chatItemFail = itemView.findViewById(R.id.chat_item_fail);
        chatItemProgress = itemView.findViewById(R.id.chat_item_progress);
    }

    @Override
    public void setData(MessageInfo data) {
        //Glide.with(mContext).load(data.getAvatar()).into(chatItemAvatar);
        chatItemAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onAvatarClick((Integer) itemView.getTag());
            }
        });
        switch (data.getFileType()) {
            case Constants.CHAT_FILE_TYPE_TEXT:
                chatItemContentText.setSpanText(handler, data.getContent(), true);
                chatItemContentText.setVisibility(View.VISIBLE);
                chatItemLayoutContent.setVisibility(View.VISIBLE);
                TextPaint paint = chatItemContentText.getPaint();
                paint.setColor(ContextCompat.getColor(mContext, R.color.chat_send_text));
                // 计算textview在屏幕上占多宽
                int len = (int) paint.measureText(chatItemContentText.getText().toString().trim());
                if (len < Utils.dp2px(mContext, 200)){
                    layoutParams.width = len + Utils.dp2px(mContext, 30);
                } else {
                    layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
                }
                layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                chatItemLayoutContent.setLayoutParams(layoutParams);
                break;
        }
        switch (data.getSendState()) {
            case Constants.CHAT_ITEM_SENDING:
                chatItemProgress.setVisibility(View.VISIBLE);
                chatItemFail.setVisibility(View.GONE);
                break;
            case Constants.CHAT_ITEM_SEND_ERROR:
                chatItemProgress.setVisibility(View.GONE);
                chatItemFail.setVisibility(View.VISIBLE);
                break;
            case Constants.CHAT_ITEM_SEND_SUCCESS:
                chatItemProgress.setVisibility(View.GONE);
                chatItemFail.setVisibility(View.GONE);
                break;
        }
    }

    public void setItemLongClick() {
        chatItemContentText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onLongClickText(v, (Integer) itemView.getTag());
                return true;
            }
        });
        chatItemLayoutContent.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onLongClickItem(v, (Integer) itemView.getTag());
                return true;
            }
        });
    }

}
